package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result list(String name,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int pageSize) {
        log.info("查询班级列表");
        LambdaQueryWrapper<Clazz> lqw = new LambdaQueryWrapper<>();
        //select * from clazz where name like ? and entrydate between begin and end
        lqw.like(name!=null&&name.length()>0,Clazz::getName,name);
        lqw.between(begin!=null&&end!=null,Clazz::getEndDate,begin,end);



        Page<Clazz> clazzPage = new Page<>(page,pageSize);
        clazzPage = clazzService.page(clazzPage,lqw);
        List<Clazz> clazzList = clazzPage.getRecords();


        //将班主任的ID和name放入map集合
        List<Emp> empList = empService.list();
        Map<Integer, String> map = empList.stream().collect(Collectors.toMap(Emp::getId, Emp::getName));

        LocalDate now = LocalDate.now();

        log.warn("now的值为",now);
        //循环遍历班级,拿到每一个班的班主任id 将map中id对应的班主任姓名设置到班级的班主任姓名中
        for (Clazz clazz : clazzList) {
            clazz.setMasterName(map.get(clazz.getMasterId()));
            if (now.isAfter(clazz.getEndDate())){
                clazz.setStatus("已结课");
            } else if (now.isBefore(clazz.getBeginDate())) {
                clazz.setStatus("未开班");
            }else{
                clazz.setStatus("在读");
            }
        }

        //select


        PageResult<Clazz> clazzPageResult = new PageResult<>(clazzPage.getTotal(), clazzPage.getRecords());



        return Result.success(clazzPageResult);
    }

    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("新增班级：{}", clazz);

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级：{}", id);

        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级：{}", clazz);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级：{}", id);

        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询全部班级");

        return Result.success();
    }
}
